<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar title="搜索结果" left-arrow @click-left="$router.go(-1)" fixed />
    </div>

    <div>
      <!-- 搜索结果-文章列表 -->
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <ArticleItem v-for="item in list" :key="item.art_id" :obj="item" @click.native="$router.push(`/article_detail?art_id=${item.art_id}`)" :isShow="isShow"></ArticleItem>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getSearchListAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      params: {
        page: 1,
        per_page: 10,
        q: '' // 关键字
      },
      list: [], // 数组
      loading: false,
      finished: false,
      isShow: false
    }
  },
  created () {
    this.params.q = this.$route.params.keyword
  },
  mounted () {
    this.getList()
  },
  methods: {
    async getList () {
      const res = await getSearchListAPI(this.params)
      console.log(res)
      this.list = [...this.list, ...res.data.data.results]

      this.loading = false

      if (res.data.data.results.length === 0) {
        this.finished = true
      }
    },
    onLoad () {
      this.params.page++
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
